/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReferencesDialog.java
 *
 * Created on 31 août 2011, 16:12:41
 */
package app.views;

import app.App;
import app.events.ReferenceSelectedListener;
import app.models.Lignevente;
import app.models.Reference;

/**
 *
 * @author PREVROMA
 */
public class ReferencesDialog extends javax.swing.JDialog implements ReferenceSelectedListener {

    private Lignevente ligneVente = null;

    /** Creates new form ReferencesDialog */
    public ReferencesDialog() {
        super(App.getInstance().getMainFrame(), true);
        setLocationRelativeTo(null);
        initComponents();
        referenceGui.addReferenceSelectedListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        referenceGui = new app.views.ReferencesGui();
        btSelect = new javax.swing.JButton();
        btAnnuler = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setTitle("Selectionner une ref");
        setAlwaysOnTop(true);

        referenceGui.setReferences(app.App.getInstance().getCatalogue().getReferences());

        btSelect.setText("Selectionner");
        btSelect.addActionListener(formListener);

        btAnnuler.setText("Annuler");
        btAnnuler.addActionListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btAnnuler)
                        .addGap(18, 18, 18)
                        .addComponent(btSelect)
                        .addContainerGap())
                    .addComponent(referenceGui, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(referenceGui, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSelect)
                    .addComponent(btAnnuler))
                .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == btSelect) {
                ReferencesDialog.this.btSelectActionPerformed(evt);
            }
            else if (evt.getSource() == btAnnuler) {
                ReferencesDialog.this.btAnnulerActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void btSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelectActionPerformed
        Reference ref = referenceGui.getSelectedReference();
        if (ref != null) {
            if(ref.getStock() <= 0){
                App.displayMessage("Pas assez de ce produit en stock", App.MESSAGE_TYPE.ERROR);
            } else {
                referenceSelected(ref);
            }
        } else {
            App.displayMessage("Faut en choisir un quand même!", App.MESSAGE_TYPE.ERROR);
        }
    }//GEN-LAST:event_btSelectActionPerformed

private void btAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnnulerActionPerformed
    this.setVisible(false);
    this.dispose();
}//GEN-LAST:event_btAnnulerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAnnuler;
    private javax.swing.JButton btSelect;
    private app.views.ReferencesGui referenceGui;
    // End of variables declaration//GEN-END:variables

    @Override
    public void referenceSelected(Reference ref) {
        LigneVenteDialog refDial = new LigneVenteDialog(ref);
        ligneVente = refDial.showDialog();
        if (ligneVente != null) {
            btAnnulerActionPerformed(null); //on ferme
        }
    }

    public Lignevente showDialog() {
        this.setVisible(true);
        return ligneVente;
    }
}
